home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 March / PCWorld_2001-03_cd.bin / Software / TemaCD / classbuild / ClassBuilder 2.2 PR405 Setup.exe / {app} / Include / CB_CriticalSection.h < prev    next >
C/C++ Source or Header  |  2000-10-17  |  1KB  |  77 lines

  1. #ifndef CB_CRITICAL_SECTION
  2. #define CB_CRITICAL_SECTION
  3.  
  4. #ifdef _WIN32
  5. // This is Windows specific, need to change this for other platforms.
  6. #include <windows.h>
  7. #include <process.h>
  8.  
  9. class CriticalSection
  10. {
  11. private:
  12.     CRITICAL_SECTION _criticalSection;
  13.  
  14. public:
  15.     CriticalSection()
  16.     {
  17.         InitializeCriticalSection(&_criticalSection);
  18.     }
  19.     ~CriticalSection()
  20.     {
  21.         DeleteCriticalSection(&_criticalSection);
  22.     }
  23.  
  24.     void Enter()
  25.     { 
  26.         EnterCriticalSection(&_criticalSection); 
  27.     }
  28.     void Leave()
  29.     { 
  30.         LeaveCriticalSection(&_criticalSection); 
  31.     }
  32. };
  33. #else
  34. // Bogus implementation of this class. We won't use multithreading so critical sections are useless.
  35.  
  36. class CriticalSection
  37. {
  38. private:
  39.     // no members
  40.  
  41. public:
  42.     CriticalSection()
  43.     {
  44.     }
  45.     ~CriticalSection()
  46.     {
  47.     }
  48.  
  49.     void Enter()
  50.     { 
  51.     }
  52.     void Leave()
  53.     { 
  54.     }
  55. };
  56.  
  57. #endif
  58.  
  59. class CriticalSectionLock
  60. {
  61. private:
  62.     CriticalSection* _criticalSection;
  63.  
  64. public:
  65.     CriticalSectionLock(CriticalSection& criticalSection)
  66.         : _criticalSection(&criticalSection) 
  67.     { 
  68.         _criticalSection->Enter();
  69.     }
  70.     ~CriticalSectionLock()
  71.     {
  72.         _criticalSection->Leave();
  73.     }
  74. };
  75.  
  76. #endif
  77.